package com.example.ble.ble_upgrade.upgrade;

import android.util.Log;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Arrays;

/**
 * Created by Admin on 2016/12/2.
 */

public class UpgradeEncodeDecode {
//    private static byte[] title=new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF};
    //98 01 21 05 00 4C EA 1C DC 27 4C EA 10 44 65 74 65 63 74 6F 72 28 76 31 2E 31 29 2D 32 30 31 38 30 35 31 35 AE
    //98 01 21 05 00 4C EA 1C DC 27 4C EA 10 44 65 74 65 63 74 6F 72 28 76 31 2E 31 29 2D 32 30 31 38 30 35 31 35 AE
    private static byte[] title=new byte[]{(byte) 0xFF,(byte) 0xFF};
    public static final byte STC = (byte) 0x9F;
    public static int said = 0xaaaaaaaa;
    static byte seq = 0;

    private static byte CMD = 0X05;
    private static byte dataSeq = 0;//升级启动  1-n升级快数 ffff 升级完成
    private static byte ack = 1;//上位机要求设备回复
    public static byte BLOCK_SIZE = (byte) 128;
    private static byte END = (byte) 0xffff;


    public static byte[] sendUpgradeData(byte seq, byte[] data, Upgrade upgrade) {
        return fbdData(seq, data, upgrade.crcByte);
    }

    public static byte[] start(Upgrade upgrade) {
        ByteBuffer headBuffer = ByteBuffer.allocate(3+upgrade.crcByte.length+ upgrade.verByte.length);
        headBuffer.order(ByteOrder.LITTLE_ENDIAN);
        headBuffer.putShort(upgrade.sizeByte);//4b ble位置变化

        headBuffer.put(upgrade.crcByte);//2b
        headBuffer.put((byte) UpgradePresenter.BLOCK_SIZE);//1b
        headBuffer.put(upgrade.verByte);
        return fbdData((byte) 0, headBuffer.array(), upgrade.crcByte);
    }

    private static byte[] fbdData(byte dataSeq, byte[] fbdData, byte[] crcByte) {
        ByteBuffer byteBuffer = ByteBuffer.allocate(5+ fbdData.length);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

        byteBuffer.put(CMD);
        byteBuffer.put(dataSeq);
        byteBuffer.put(crcByte);
        if (fbdData != null) {
            byteBuffer.put((byte) fbdData.length);
            byteBuffer.put(fbdData);
        }
        return send7e(byteBuffer.array());
    }


    private static byte[] send7e(byte[] data) {
        ByteBuffer b = ByteBuffer.allocate(4+title.length + data.length);
        b.order(ByteOrder.LITTLE_ENDIAN);
        b.put(title);
        b.put(STC);
        seq++;
        b.put(seq);
        if (data != null) {
            b.put((byte) data.length);
            b.put(data);
        }
        b.put(getSum(b.array()));

        String result = "";
        for (int i = 0; i < b.array().length; i++) {
            result += String.format("%02X ", b.array()[i]);
        }
        Log.d("send7E------->", result);
        return b.array();
    }


    public static UpgradeReponse decode7e(byte[] data) {


        ByteBuffer byteBuffer = ByteBuffer.wrap(data);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);

        byte stc = byteBuffer.get();
        if (stc!=STC){
            return null;
        }
        byte seq = byteBuffer.get();
        byte len = byteBuffer.get();
        byte[] fbdData = new byte[len];
        byteBuffer.get(fbdData);
        byte sum = byteBuffer.get();
        return decode7eFbd(fbdData);

    }

    private static UpgradeReponse decode7eFbd(byte[] fbdData) {
        UpgradeReponse upgradeReponse = new UpgradeReponse();
        ByteBuffer byteBuffer = ByteBuffer.wrap(fbdData);
        byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
        byte cmd = byteBuffer.get();//应该=05
        byte blockSize = byteBuffer.get();//设备索要的升级包
        short crc = byteBuffer.getShort();
        byte len = byteBuffer.get();
        if (blockSize == 0) {
            //错误
            upgradeReponse.hasError = true;
        } else {
            upgradeReponse.block = blockSize;
        }
        return upgradeReponse;
    }

    public static byte[] hexStringToByte(String hex) {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++) {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    private static byte toByte(char c) {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    public static byte[] intToBytes(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    public static byte[] intToBytes3(int value) {
        byte[] src = new byte[3];
        src[0] = (byte) ((value >> 16) & 0xFF);
        src[1] = (byte) ((value >> 8) & 0xFF);
        src[2] = (byte) (value & 0xFF);
        return src;
    }
    public static byte getSum(byte[] byteBufferData) {
        byte cs = 0;
        for (int i = title.length; i < byteBufferData.length; i++) {
            cs += byteBufferData[i];
        }
        return cs;
    }

    public static String getDataString(byte[] data){
        String result = "";
        for (int i = 0; i < data.length; i++) {
            result += String.format("%02X ", data[i]);
        }
        return result;

    }
}
